<?php
/*
Template Name: Page: Archives
*/
?>
<?php $theme_options = get_option('po_theme'); ?>
<?php
global $wpdb;

    $SQL = "
    SELECT
        ID
        ,post_title
        ,YEAR(post_date) AS `year`
        ,MONTH(post_date) AS `month`
    FROM {$wpdb->posts}
    WHERE post_type = 'post'
    AND post_status = 'publish'
    ORDER BY post_date DESC";

$AllPosts = $wpdb->get_results($SQL);

// all months
$MonthAbr   = array();
$MonthNames = array();
for ($i = 1; $i <= 12; $i++) {
    $MonthAbr[$i]   = ucfirst(strftime(__("%b",''), strtotime($i."/01/2001")));
    $MonthNames[$i] = ucfirst(strftime(__("%B",''), strtotime($i."/01/2001")));
}
// posts to our format
$Archive = array();
foreach ($AllPosts as $Post) {
    if (!isset($Archive[$Post->year][$Post->month])) $Archive[$Post->year][$Post->month] = array();
    $Archive[$Post->year][$Post->month][] = array('ID'=>$Post->ID, 'title'=>$Post->post_title);
}
get_header(); ?>
	<div id="content">
	<div id="cc">
		<div id="bread" class="mb-10">
			<div id="breadbox">
				<?php // BREADCRUMBS
					if (function_exists('yoast_breadcrumb')) {yoast_breadcrumb('<div id="breadcrumbs">','</div>');}
				?>
				<div class="clear"><!-- --></div>
			</div><!-- end breadbox -->
		</div>
		<div id="contentbox" class="w620">

		<div id="title">
			<h1 class="bline"><?php the_title(); ?></h1>
		</div><!-- end title -->

        <?php while (have_posts()) : the_post(); ?>
			<?php foreach ($Archive as $Year => $Month) : ?>
				<h2><a href="<?php echo get_year_link($Year)?>"><?php echo $Year ?></a></h2>
					<?php for ($i = 12; $i >= 1; $i--) : ?>
					<?php if (isset($Archive[$Year][$i])) : ?>
					<h3><a href="<?php echo get_month_link($Year, $i)?>"><?php echo $MonthNames[$i].' '.$Year ?></a></h3>
						<ul>
							<?php foreach ($Month[$i] as $Post) : ?>
							<li><a href="<?php echo get_permalink($Post['ID']) ?>"><?php echo $Post['title'] ?></a></li>
							<?php endforeach; ?>
						</ul>
					<?php endif; ?>
					<?php endfor; ?>
			<?php endforeach; ?>
        <?php endwhile; ?>

		</div><!-- end contentbox -->
		
			<ul id="sidebar">
				<?php // DISPLAY CUSTOM SIDEBAR
				if ($sidebar) { if ( function_exists('dynamic_sidebar') && dynamic_sidebar($sidebar)) ;}
				else { if (function_exists('dynamic_sidebar') && dynamic_sidebar('Default Sidebar')) ;}
				?>
			</ul><!-- end sidebar -->

<?php get_footer(); ?>